<template>
  <div id="app">
    <HeaderNav
      :active-section="activeSection"
      @section-click="handleSectionClick"
    />

    <main class="main">
      <HeroSection />
      <AboutSection />
      <ProjectsSection />
      <ContactSection />
    </main>

    <FooterSection />

    <!-- 返回顶部按钮 -->
    <div
      class="back-to-top"
      :class="{ 'back-to-top-visible': showBackToTop }"
      @click="scrollToTop"
    >
 
    </div>

    <!-- 加载动画 -->
    <div v-if="isLoading" class="loading-overlay">
      <div class="loading-spinner"></div>
      <p class="loading-text">正在加载...</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import HeaderNav from './components/HeaderNav.vue'
import HeroSection from './components/HeroSection.vue'
import AboutSection from './components/AboutSection.vue'
import ProjectsSection from './components/ProjectsSection.vue'
import ContactSection from './components/ContactSection.vue'
import FooterSection from './components/FooterSection.vue'

const activeSection = ref('home')
const showBackToTop = ref(false)
const isLoading = ref(true)

// 监听滚动事件
const handleScroll = () => {
  const scrollPosition = window.scrollY
  showBackToTop.value = scrollPosition > 300

  // 更新当前活跃的section
  const sections = ['home', 'about', 'projects', 'contact']
  for (const section of sections) {
    const element = document.getElementById(section)
    if (element) {
      const rect = element.getBoundingClientRect()
      if (rect.top <= 100 && rect.bottom >= 100) {
        activeSection.value = section
        break
      }
    }
  }
}

// 处理section点击
const handleSectionClick = (sectionId: string) => {
  activeSection.value = sectionId
}

// 返回顶部
const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth'
  })
}

// 模拟页面加载
const simulateLoading = () => {
  setTimeout(() => {
    isLoading.value = false
  }, 1500)
}

onMounted(() => {
  // 监听滚动事件
  window.addEventListener('scroll', handleScroll)

  // 模拟加载
  simulateLoading()

  // 添加CSS变量支持
  document.documentElement.style.setProperty('--primary-color', '#667eea')
  document.documentElement.style.setProperty('--secondary-color', '#764ba2')
  document.documentElement.style.setProperty('--text-primary', '#2d3748')
  document.documentElement.style.setProperty('--text-secondary', '#718096')
  document.documentElement.style.setProperty('--bg-primary', '#ffffff')
  document.documentElement.style.setProperty('--bg-secondary', '#f7fafc')
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})
</script>

<style>
/* 全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  scroll-padding-top: 80px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--bg-primary);
  overflow-x: hidden;
}

/* CSS变量定义 */
:root {
  --primary-color: #667eea;
  --secondary-color: #764ba2;
  --text-primary: #2d3748;
  --text-secondary: #718096;
  --bg-primary: #ffffff;
  --bg-secondary: #f7fafc;
  --border-color: #e2e8f0;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.1);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1);
  --shadow-xl: 0 20px 25px rgba(0, 0, 0, 0.1);
  --radius-sm: 0.375rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-xl: 1rem;
  --transition-fast: 0.15s ease;
  --transition-normal: 0.3s ease;
  --transition-slow: 0.5s ease;
}

/* 主内容区域 */
.main {
  margin-top: 80px;
}

/* 返回顶部按钮 */
.back-to-top {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
}

.back-to-top-visible {
  opacity: 1;
  visibility: visible;
}

.back-to-top-icon {
  width: 50px;
  height: 50px;
  background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 1.2rem;
  box-shadow: var(--shadow-lg);
  transition: all 0.3s ease;
}

.back-to-top:hover .back-to-top-icon {
  transform: translateY(-3px);
  box-shadow: var(--shadow-xl);
}

/* 加载动画 */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  transition: opacity 0.5s ease;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top: 3px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 1rem;
}

.loading-text {
  color: white;
  font-size: 1.1rem;
  opacity: 0.9;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes scaleIn {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* 通用动画类 */
.animate-fade-in {
  animation: fadeInUp 0.6s ease-out;
}

.animate-slide-in-left {
  animation: slideInLeft 0.6s ease-out;
}

.animate-slide-in-right {
  animation: slideInRight 0.6s ease-out;
}

.animate-scale-in {
  animation: scaleIn 0.6s ease-out;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .back-to-top {
    bottom: 1rem;
    right: 1rem;
  }

  .back-to-top-icon {
    width: 40px;
    height: 40px;
    font-size: 1rem;
  }

  :root {
    --radius-lg: 0.5rem;
    --radius-xl: 0.75rem;
  }
}

/* 暗色模式支持 */
@media (prefers-color-scheme: dark) {
  :root {
    --text-primary: #f7fafc;
    --text-secondary: #a0aec0;
    --bg-primary: #1a202c;
    --bg-secondary: #2d3748;
    --border-color: #4a5568;
  }
}

/* 减少动画偏好 */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  html {
    scroll-behavior: auto;
  }
}
</style>

<style scoped>
#app {
  min-height: 100vh;
}
</style>

<style lang="scss">
// SCSS变量和混合器
$breakpoints: (
  'sm': 640px,
  'md': 768px,
  'lg': 1024px,
  'xl': 1280px
);

@mixin respond-to($breakpoint) {
  @media (min-width: map-get($breakpoints, $breakpoint)) {
    @content;
  }
}

// 玻璃拟态效果混合器
@mixin glassmorphism($opacity: 0.1, $blur: 10px) {
  background: rgba(255, 255, 255, $opacity);
  backdrop-filter: blur($blur);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

// 渐变背景混合器
@mixin gradient-bg($start-color: var(--primary-color), $end-color: var(--secondary-color)) {
  background: linear-gradient(135deg, $start-color 0%, $end-color 100%);
}

// 卡片阴影混合器
@mixin card-shadow {
  box-shadow: 0 8px 32px rgba(31, 38, 135, 0.1);
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
    transform: translateY(-5px);
  }
}
</style>